package com.tripcube.ai.service.impl;

import com.aliyun.bailian20231229.Client;
import com.aliyun.bailian20231229.models.*;
import com.aliyun.teautil.models.RuntimeOptions;
import com.github.pagehelper.PageInfo;
import com.tripcube.ai.model.dto.CategoryCreatDTO;
import com.tripcube.ai.model.dto.CategorySelectDTO;
import com.tripcube.ai.model.entity.Category;
import com.tripcube.ai.service.CategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    private final Client bailianClient;

    @Value("${alibaba.cloud.workspace-id}")
    private String workspaceId;

    public String getCategoryName(String categoryId) {
        return "categoryName";
    }

    @Override
    public Category addCategory(CategoryCreatDTO categoryCreatDTO) {

        AddCategoryRequest request = new AddCategoryRequest();
        request.setCategoryName(categoryCreatDTO.getCategoryName());
        request.setCategoryType(categoryCreatDTO.getCategoryType());
        request.setParentCategoryId(categoryCreatDTO.getParentCategoryId());

        RuntimeOptions runtime = new RuntimeOptions();
        Map<String, String> headers = new HashMap<>();
        AddCategoryResponse response = null;
        try {
            response = bailianClient.addCategoryWithOptions(workspaceId, request, headers, runtime);
        } catch (Exception e) {
            log.error("创建类目失败", e);
        }

        log.info("创建类目成功");
        Category category = new Category();
        if (response != null) {
            category.setCategoryId(response.getBody().getData().getCategoryId());
            category.setCategoryName(response.getBody().getData().getCategoryName());
        }
        return category;

    }

    @Override
    public boolean deleteCategory(String categoryId) {
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        DeleteCategoryResponse response = null;
        try {
            response = bailianClient.deleteCategoryWithOptions(categoryId, workspaceId, headers, runtime);
        } catch (Exception e) {
            log.error("删除类目失败", e);
            throw new RuntimeException(e);
        }

        return response.body.getSuccess();
    }

    @Override
    public int deleteCategoryBatch(String[] categoryIds) {
        int count = 0;
        for(String categoryId : categoryIds){
            if(deleteCategory(categoryId)){
                count++;
            }
        }
        return count;
    }

    @Override
    public ListCategoryResponseBody listCategory(CategorySelectDTO categorySelectDTO) {

        ListCategoryRequest request = new ListCategoryRequest();
        request.setCategoryName(categorySelectDTO.getCategoryName());
        request.setCategoryType(categorySelectDTO.getCategoryType());
        request.setParentCategoryId(categorySelectDTO.getParentCategoryId());
        request.setNextToken(categorySelectDTO.getNextToken());
        request.setMaxResults(categorySelectDTO.getMaxResults());

        Map<String, String> headers = new HashMap<>();
        RuntimeOptions runtime = new RuntimeOptions();

        ListCategoryResponse response = null;
        try {
            response = bailianClient.listCategoryWithOptions(workspaceId, request, headers, runtime);
        } catch (Exception e) {
            log.error("查询类目失败", e);
        }

        if (response != null) {
            return response.body;
        }
        return new ListCategoryResponseBody();
    }

}
